package com.helloniu.fl.util {

import flash.utils.ByteArray;
import com.gnetis.tang.util.Class3;
import com.gnetis.tang.whiteboard.WhiteboardObject;

public class Tool 
{
	private static var TANG_NULL:int = 0;
	private static var TANG_OBJECT:int = 1;
	   
	public static function writeDate(output:ByteArray, date:Date):void {
		if(date == null) {
			output.writeByte(TANG_NULL);
		}
		else {
			output.writeByte(TANG_OBJECT);
			var ms:Number = date.getTime();
			output.writeDouble(ms);
		}
	}
	
	public static function readDate(input:ByteArray):Date {
		var b:int = input.readByte();
    	
    	if(b == TANG_NULL) {
    	   return null;	
    	}
    	
		var ms:Number = input.readDouble();
		var date:Date = new Date(ms);

		return date;
	}
	
	public static function writeObject(output:ByteArray, obj:WhiteboardObject):void {
		if(obj == null) {
			output.writeByte(TANG_NULL);
		}
		else {
			output.writeByte(TANG_OBJECT);
			output.writeUTF(obj.classname);
			obj.writeData(output);
		}
	}
	
	public static function readObject(input:ByteArray):WhiteboardObject {
		var b:int = input.readByte();
		
		var obj:WhiteboardObject = null;
    	
    	if(b == TANG_NULL) {
    	   return obj;	
    	}
    	
    	var clazz:String = input.readUTF();

		try {
			obj = Class3.newInstance(clazz) as WhiteboardObject;
			obj.readData(input);		
		}
		catch(ex:Error) {
			// @temp tonyy
			// log the error ? or do sth?
			throw ex;
		}

		return obj;
	}
		
	public static function writeArray(output:ByteArray, arr:Array):void {
		if(arr == null || arr.length == 0) {
			output.writeInt(0);
		}
		else {
			output.writeInt(arr.length);
			
			for(var i:int = 0; i < arr.length; i++) {
				if(!(arr[i] is WhiteboardObject)) {
					// @temp tonyy
					var e:Error = new Error("Unexpected object found: " + arr[i]);
					trace(e.getStackTrace());
					throw e;
				}
				
				var obj:WhiteboardObject = arr[i] as WhiteboardObject;
				
				writeObject(output, obj);
			}
		}
	}
	
	public static function readArray(input:ByteArray):Array {
		var arrLength:int = input.readInt();
		
		var arr:Array = new Array();
    	
    	if(arrLength == 0) {
    	   return arr;	
    	}
    	
    	for(var i:int = 0; i < arrLength; i++) {
    		arr.push(readObject(input));
    	}
    	
		return arr;
	}	
	
	public static function objectToString(obj:WhiteboardObject):String {
		var barr:ByteArray = new ByteArray();
		obj.writeData(barr);		
		barr.compress();
    	var javaEncode:String = Base64.encode(barr);
    	return javaEncode;	
	}
	
	public static function stringToObject2(obj:WhiteboardObject, str:String):void {
		var barr:ByteArray = Base64.decode(str);
		barr.uncompress();
        barr.position = 0;
        obj.readData(barr);
	}
	
	public static function writeColor(output:ByteArray, color:Number, alpha:Number = 0):void {
		var r:Number = ((color >> 16) & 0xFF);
		var g:Number = ((color >> 8) & 0xFF);
		var b:Number = (color & 0xFF);
		output.writeInt(r);
		output.writeInt(g);
		output.writeInt(b);
		output.writeInt(alpha);
	}
	
	public static function readColor(input:ByteArray):Array {
		var r:Number = input.readInt();
	    var g:Number = input.readInt();
	    var b:Number = input.readInt();
	    var a:Number = input.readInt();
	    
	    var color:Number = (r << 16) | (g << 8) | b;
	    
		return new Array(color, a);
	}
}
}